package com.sendi.system.action;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONObject;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.BeanUtils;

import com.sendi.system.entity.SysBeanMap;
public class SysLogBeanmapAction extends SysAction {
    private Integer  id;
  	private String beanName;
	private String beamBsname;
	private String islog;
	private String remark;
	
	public SysLogBeanmapAction()
	{
		super();
		data_session_name="SysLogBeanmapActionData";
	}
	
	
	public void sysLogBeanmapInit()
	{
		List datas=null;
		
		if(getSession().getAttribute(data_session_name)!=null)
		{
			datas=(List)getSession().getAttribute(data_session_name);
			System.out.println("use session size"+datas.size());
		}
		else
		{
			addSysLogBeanmapTosessin();
			datas=(List)getSession().getAttribute(data_session_name);
		}
		 
			writeJSONArray(datas);
		 
	}
	
	public void addSysLogBeanmapTosessin()
	{
		List datas=null;
		String hql="from  SysBeanMap";
	 	datas=serviceSupportImp.findByHQL(hql);
	  	getSession().setAttribute(data_session_name,datas);
	 	 
	}
	
 
   /*
    * 
    */
	public void querysysLogBeanmap()
	{
		   try
		   {
			     Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
			 	StringBuffer hql=new StringBuffer("from SysBeanMap where 1=1 ");
				if(beanName!=null&&beanName.trim().length()>0)
				{
					hql.append(" and  beanName like :beanName ");
				}
				if(beamBsname!=null&&beamBsname.trim().length()>0)
				{
					hql.append(" and beamBsname like :beamBsname");
				}
				
				   Query query= session.createQuery(hql.toString());
				   if(beanName!=null&&beanName.trim().length()>0)
					{
					  query.setParameter("beanName", "%"+beanName+"%");
					}
					if(beamBsname!=null&&beamBsname.trim().length()>0)
					{
				 		  query.setParameter("beamBsname", "%"+beamBsname+"%");
					}
					   List datas=query.list();
					    System.out.println("query size"+datas.size());
				     	getSession().setAttribute(data_session_name,datas);	
				     	writeResponseText("{success:true,msg:'success',datasize:"+datas.size()+"}");
		   			}
		   catch (Exception e) {
			   writeResponseText("{success:false,msg:'failure'}");
				 
		}
		    
		
	}

 
	 public void addsysLogBeanmap()
	 {
		 
		 
		 SysBeanMap  sysbeanmpa=new SysBeanMap(beanName,  beamBsname, remark, islog);
		 	try {
				serviceSupportImp.save(sysbeanmpa);
				getSession().removeAttribute(data_session_name);
				addSysLogBeanmapTosessin();
				writeResponseText("{success:true,msg:'success'}");
		 	} catch (Exception e) {
				writeResponseText("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
			  e.printStackTrace();
			}
	 }

	 
	 public void excutModifyQuerysysLogBeanmap()
		{
			System.out.println("excutModifyQuery...."+id);
			String hql="from SysBeanMap where 1=1 and id="+this.id; 
			
			List datas=serviceSupportImp.findByHQL(hql);
		 
		 	System.out.println("data size:"+datas.size());
		 	SysBeanMap sysLogBeanmap=(SysBeanMap)datas.get(0);
			try {
			 	writeResponseText("{success:true,datas:"+JSONObject.fromObject(sysLogBeanmap).toString()+"}");
			} catch (Exception e) {
				try {
					getResponse().getWriter().write("{success:false,msg:'faild',errors:{"+e.getMessage()+"}}");
				} catch (IOException e1) {
				 
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
			
		}
		public void excuteModifysysLogBeanmap()
		{
			System.out.println("excuteModify...."+id);
			String hql="from SysBeanMap where 1=1 and id="+this.id; 
			
			List datas=serviceSupportImp.findByHQL(hql);
		 
		 	System.out.println("data size:"+datas.size());
		 	SysBeanMap sysLogBeanmap=(SysBeanMap)datas.get(0);
		 	SysBeanMap source=new SysBeanMap(beanName,  beamBsname,remark,  islog);
		 	 String exclude[]={"id"};
		 	 BeanUtils.copyProperties(source, sysLogBeanmap,exclude);
		 	serviceSupportImp.update(sysLogBeanmap);
		 	getSession().removeAttribute(data_session_name);
			addSysLogBeanmapTosessin();
			writeResponseText("{success:true,msg:'success'}"); 
		}
		
		public void excuteDeletesysLogBeanmap()
		{
		 String hql="delete SysBeanMap where id in ("+deleterecord+")";
		 Session session=serviceSupportImp.getDao().getSessionFactory().openSession();
		  session.beginTransaction();
		  session.createQuery(hql).executeUpdate();
		  session.getTransaction().commit();
		  addSysLogBeanmapTosessin();
		  writeResponseText("{success:true,msg:'success'}"); 
		}
	public Integer getId() {
		return id;
	}



	public void setId(Integer id) {
		this.id = id;
	}


	public String getBeanName() {
		return beanName;
	}


	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}


	public String getBeamBsname() {
		return beamBsname;
	}


	public void setBeamBsname(String beamBsname) {
		this.beamBsname = beamBsname;
	}


	public String getIslog() {
		return islog;
	}


	public void setIslog(String islog) {
		this.islog = islog;
	}


	public String getRemark() {
		return remark;
	}


	public void setRemark(String remark) {
		this.remark = remark;
	}



 

 

}
